public class Solution {
    public String simplifyPath(String path) {
        LinkedList<String> stack = new LinkedList<>();
        String[] strs = path.split("/");
        StringBuilder sb = new StringBuilder("");
        for(String str : strs) {
            if(!str.equals("")) {
                if(str.equals("..")) {
                    if(!stack.isEmpty()) {
                        stack.pop();
                    }
                } else if(!str.equals(".")) {
                    stack.push(str);
                }
            }
        }
        if(stack.isEmpty()) {
            return "/";
        }
        while(!stack.isEmpty()) {
            sb.insert(0, stack.poll());
            sb.insert(0, "/");
        }
        return sb.toString();
    }
}